
/**
 * \class currentEventCoordinates.
 * \brief This class gets current coordinates and stores them.
 * \Author Salman Noor.
 */

/**
 *
 * \brief Global variables.
 */
var eventCoorindateArray = new Array();
var eventCoordinates = new currentEventCoordinates();


function currentEventCoordinates(lat,lng)
{
    this.eventLatitude=lat;
    this.eventLongitude=lng;
   
    this.reinitialize = function(lat,lng)
    {
        this.eventLatitude=lat;
        this.eventLongitude=lng;
    }

    this.listEventCoordinates =function()
    {
        var timeItem = "<div id='selectEventCoordinates'> Geo-Latitude : "+this.eventLatitude+"  --  Geo-Longitude : "+this.eventLongitude
        +"   --  <button id='deleteCurrentEventCoordinates' class='deleteEventLocation'>Delete</button> </div>";


        return timeItem;
    }

    this.emptyObject = function()
    {
        this.eventLatitude=null;
        this.eventLongitude=null;
    }

}

var selectedTab =   jQuery("#businessDiv").children("#tabs").tabs( "option", "selected" );

/*
 * \brief This javascript function parses the html content into jquery UI elements
 */

$(function(){
    $( "#selectable" ).selectable();
    $("#tabs").tabs();


    jQuery("#businessDiv").children("#tabs").tabs('select', 2);
    jQuery("#businessDiv").children("#tabs").bind('tabsselect', function(event, ui) {
        selectedTab = ui.index;
        if (selectedTab == 3)
        {
            window.location.href="http://localhost/stellogoogle/eventsmanager/";
            jQuery("#businessDiv").slideUp('slow');
            jQuery("#homeDiv").show('slow');
            jQuery("#businessDiv").empty();
        }
    });

    $("#updateUserDetails").button({

        icons: {
            primary: 'ui-icon-pencil',
            secondary: 'ui-icon-pencil'
        },
        text: true
    });
                    
    $( "#alterationOptions button:first" ).button({
        icons: {
            primary: "ui-icon-zoomin",
            secondary :"ui-icon-zoomin"
        },
        text: true
    }).next().button({
        icons: {
            primary: "ui-icon-trash",
            secondary :"ui-icon-trash"

        },
        text: true
    });
    $( "#viewEvent" ).button( "option", "disabled", true );
    $( "#deleteEvent" ).button( "option", "disabled", true );



    $( "#selectableCurrentEventList" ).selectable({
        stop: function() {
            var result = $( "#select-result" ).empty();
            $( ".ui-selected", this ).each(function() {
                var index = $( "#selectableCurrentEventList li" ).index( this );
                var eventID = $(this).attr('title');
                var eventName = $(this).html();
                var eventRelIndex = index + 1;
                if (eventRelIndex <= 0)
                {

                    $( "#viewEvent" ).button( "option", "disabled", true );
                    $( "#deleteEvent" ).button( "option", "disabled", true );
                }
                else
                {
                    $( "#viewEvent" ).button( "option", "disabled", false );
                    $( "#deleteEvent" ).button( "option", "disabled", false );
                    $( "#viewEvent" ).unbind();
                    $( "#deleteEvent" ).unbind();
                    $( "#viewEvent" ).click(function(){
                        viewEventInDetail(eventID);
                    });

                    $( "#deleteEvent" ).click(function(){
                        deleteEvent(eventID, eventName);
                    });
                }
                result.append(eventName);
            });
        }
    });
    $("#dialog-singleEventDetails").dialog({
        width: 840,
        height: 840,
        modal: true,
        show: 'clip',
        hide: 'clip',
        autoOpen: false,
        closeOnEscape: true,
        buttons: {
            'OK': function() {
                $(this).dialog('close');
            }
        }
    });


    $("#dialog-deleteEventOK").dialog({
        resizable: true,
        width: 740,
        modal: true,
        show: 'clip',
        hide: 'clip',
        autoOpen: false,
        closeOnEscape: true,
        buttons: {
            'OK': function() {
                $(this).dialog('close');
            }
        }
    });

    $("#dialog-deleteConfirm").dialog({
        width: 740,
        modal: true,
        show: 'clip',
        hide: 'clip',
        autoOpen: false,
        closeOnEscape: true,
        buttons: {
            'Delete Event': function() {
                sendRequest.setAction('deleteEvent');
                eventIDInfo = {
                    'eventID':eventID
                };
                sendRequest.setSendParameters(eventIDInfo);

                $("#errorMessageDiv").load(sendRequest.serverURL, sendRequest.getSendData(), function(){

                    });
            },
            'Cancel': function() {
                $(this).dialog('close');
            }
        }
    });



    $("#updateUserDetails").click(function(){

        jQuery( "#dialog-box-registration").dialog({
            title: 'Update User Details'
        });
        var firstName = jQuery(':input[id=firstName]');
        var lastName = jQuery(':input[id=lastName]');
        var userName = jQuery(':input[id=userName]');
        var formEmail = jQuery(':input[id=emailAddress]');
        var formPassword = jQuery(':input[id=passwordField]');
        var formRepeatPassword = jQuery(':input[id=repeatPasswordField]');
        firstName.val($("input:hidden[name=userFirstName]").val());
        lastName.val($("input:hidden[name=userLastName]").val());
        userName.val($("input:hidden[name=userUserName]").val());
        formEmail.val($("input:hidden[name=userEmail]").val());
        formPassword.val($("input:hidden[name=userPassword]").val());
        formRepeatPassword.val($("input:hidden[name=userPassword]").val());
        var hiddenResponseDiv = jQuery('.responseDiv');
        hiddenResponseDiv.hide();
        var allFields = jQuery([]).add(firstName).add(lastName).add(userName).add(formEmail).add(formPassword).add(formRepeatPassword);
        $('#container_userType').buttonset({
            disabled: true
        });
        $("#dialog-box-registration").dialog('open');
    
        $("#dialog-box-registration").dialog( "option", "buttons", {
            "Update": function() {
                // jQuery(this).dialog("close");
                var bValid = true;
                allFields.removeClass('ui-state-error');

                bValid = bValid && formValidator.checkLength(firstName,"your first name",2,20);
                bValid = bValid && formValidator.checkRegexp(firstName,/^([0-9a-zA-Z])+$/,"The first name field only allows alphanumeric characters (a-z 0-9).");
                bValid = bValid && formValidator.checkLength(lastName,"your last name",2,20);
                bValid = bValid && formValidator.checkRegexp(lastName,/^([0-9a-zA-Z])+$/,"The last name field only allows alphanumeric characters (a-z 0-9).");
                bValid = bValid && formValidator.checkRegexp(formEmail,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Enter a valid email address, eg. yourname@wits.ac.za.");
                bValid = bValid && formValidator.checkLength(userName,"your user name",1,20);
                bValid = bValid && formValidator.checkRegexp(userName,/^([0-9a-zA-Z])+$/,"The user name field only allows alphanumeric characters (a-z 0-9).");
                bValid = bValid && formValidator.checkLength(formPassword,"your password",1,20);
                bValid = bValid && formValidator.checkRegexp(formPassword,/^([0-9a-zA-Z])+$/,"The password field only allows alphanumeric characters (a-z 0-9).");
                bValid = bValid && formValidator.checkLength(formRepeatPassword,"your password",1,20);
                bValid = bValid && formValidator.checkRegexp(formRepeatPassword,/^([0-9a-zA-Z])+$/,"The password field only allows alphanumeric characters (a-z 0-9).");
                bValid = bValid && formValidator.checkPasswordFields(formPassword,formRepeatPassword);

                if (bValid) {
                    sendRequest.setAction('updateBUserDetails');

                    formData = {
                        'firstName':firstName.val(),
                        'lastName':lastName.val(),
                        'formEmail':formEmail.val(),
                        'userName':userName.val(),
                        'formPassword':formPassword.val()
                        };
                    sendRequest.setSendParameters(formData);

                    hiddenResponseDiv.load(sendRequest.serverURL, sendRequest.getSendData(), function(){
                        hiddenResponseDiv.unbind();
                        if (hiddenResponseDiv.html() == '1')
                        {
                            jQuery("#dialog-box-registration").dialog('close');
                            jQuery('.errorMessageDiv').html('');
                            jQuery("#fnameListContainer").html(firstName.val());
                            jQuery("#lnameListContainer").html(lastName.val());
                            jQuery("#uemailListContainer").html(formEmail.val());
                            jQuery("#unameListContainer").html(userName.val());

                            $("input:hidden[name=userFirstName]").val(firstName.val());
                            $("input:hidden[name=userLastName]").val(lastName.val());
                            $("input:hidden[name=userUserName]").val(userName.val());
                            $("input:hidden[name=userEmail]").val(formEmail.val());
                            $("input:hidden[name=userPassword]").val(formPassword.val());

                            jQuery("#dialogue-registrationOKMessage").dialog('open');
                            jQuery('input[id=firstName]').val('');
                            jQuery('input[id=lastName]').val('');
                            jQuery('input[id=userName]').val('');
                            jQuery('input[id=emailAddress]').val('');
                            jQuery('input[id=passwordField]').val('');
                            jQuery('input[id=repeatPasswordField]').val('');
                        }
                        else
                        {
                            userName.addClass('ui-state-error');
                            formValidator.updateErrorMessage("User Name Already Exists. Please Select another User Name");
                        }
                    });
                }
            },
            "Cancel": function() {
                jQuery(this).dialog('close');
                jQuery('.errorMessageDiv').html('');
                jQuery('input[id=firstName]').val('');
                jQuery('input[id=lastName]').val('');
                jQuery('input[id=userName]').val('');
                jQuery('input[id=emailAddress]').val('');
                jQuery('input[id=passwordField]').val('');
                jQuery('input[id=repeatPasswordField]').val('');
            }
        });

    });

});


function deleteEvent(eventID, eventName)
{
    $("#dialog-deleteConfirm").dialog({
        //resizable: true,
        width: 740,
        modal: true,
        show: 'clip',
        hide: 'clip',
        autoOpen: false,
        closeOnEscape: true,
        buttons: {
            'Delete Event': function() {
                $('li[title='+eventID+']').hide('slow');
                $(this).dialog('close');
                $("#dialog-deleteEventOK").dialog('open');
            },
            'Cancel': function() {
                $(this).dialog('close');
            }
        }
    });
    $("#dialog-deleteConfirm").dialog('open');
}
function viewEventInDetail(eventID)
{

    sendRequest.setAction('getDetailsForEvent');
    eventIDInfo = {
        'eventID':eventID
    };
    sendRequest.setSendParameters(eventIDInfo);
    $("#detailsForEventContainer").load(sendRequest.serverURL, sendRequest.getSendData(), function(){
        $("#detailsForEventContainer").unbind();
        $( "#selectableEventTypeGrid" ).selectable();
        $("#dialog-singleEventDetails").dialog({
            resizable: true,
            width: 840,
            height: 840,
            modal: true,
            show: 'clip',
            hide: 'clip',
            autoOpen: false,
            closeOnEscape: true,
            open: function(){
                $( "#actvityDetailsAccordion" ).accordion({
                    event: "mouseover",
                    autoHeight: true
                });
            },
            buttons: {
                'OK': function() {
                    $(this).dialog('close');
                }
            }
        });
		

        $("#dialog-singleEventDetails").dialog('open');
    });

}